Passed
Push — master ( 0562f2...7d3761 )
by Rafael S.
01:24
created

read-bytes.js ➔ read64BitFloat   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
/*
2
 * read-bytes: Function to read data from bytes.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
let helpers = require("../src/helpers.js");
8
const floats = require("../src/floats.js");
9
const intBits = require("int-bits");
10
11
/**
12
 * Read a group of bytes by turning it to bits.
13
 * Useful for 40 & 48-bit, but underperform.
14
 * TODO find better alternative for 40 & 48-bit.
15
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
16
 * @param {number} i The index to read.
17
 * @param {number} numBytes The number of bytes
18
 *      (1 for 8-bit, 2 for 16-bit, etc).
19
 * @return {number}
20
 */
21
function readBytesAsBits(bytes, i, numBytes) {
22
    let j = numBytes-1;
23
    let bits = "";
24
    while (j >= 0) {
25
        bits += helpers.bytePadding(bytes[j + i].toString(2), 2);
26
        j--;
27
    }
28
    return parseInt(bits, 2);
29
}
30
31
/**
32
 * Read 1 1-bit int from from booleans.
33
 * @param {!Array<number>|Uint8Array} bytes An array of booleans.
34
 * @param {number} i The index to read.
35
 * @return {number}
36
 */
37
function read1Bit(bytes, i) {
38
    return parseInt(bytes[i], 2);
39
}
40
41
/**
42
 * Read 1 8-bit int from from bytes.
43
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
44
 * @param {number} i The index to read.
45
 * @return {number}
46
 */
47
function read8Bit(bytes, i) {
48
    return bytes[i];
49
}
50
51
/**
52
 * Read 1 16-bit int from from bytes.
53
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
54
 * @param {number} i The index to read.
55
 * @return {number}
56
 */
57
function read16Bit(bytes, i) {
58
    return bytes[1 + i] << 8 | bytes[i];
59
}
60
61
/**
62
 * Read 1 16-bit float from from bytes.
63
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
64
 * @param {number} i The index to read.
65
 * @return {number}
66
 */
67
function read16BitFloat(bytes, i) {
68
    return floats.decodeFloat16(bytes.slice(i,i+2));
69
}
70
71
/**
72
 * Read 1 24-bit int from from bytes.
73
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
74
 * @param {number} i The index to read.
75
 * @return {number}
76
 */
77
function read24Bit(bytes, i) {
78
    return bytes[2 + i] << 16 |
79
        bytes[1 + i] << 8 |
80
        bytes[i];
81
}
82
83
/**
84
 * Read 1 32-bit int from from bytes.
85
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
86
 * @param {number} i The index to read.
87
 * @return {number}
88
 */
89
function read32Bit(bytes, i) {
90
    return (bytes[3 + i] << 24 |
91
        bytes[2 + i] << 16 |
92
        bytes[1 + i] << 8 |
93
        bytes[i]) >>> 0;
94
}
95
96
/**
97
 * Read 1 32-bit float from from bytes.
98
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
99
 * @param {number} i The index to read.
100
 * @return {number}
101
 */
102
function read32BitFloat(bytes, i) {
103
    return intBits.pack(read32Bit(bytes, i));
104
}
105
106
/**
107
 * Read 1 40-bit int from from bytes.
108
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
109
 * @param {number} i The index to read.
110
 * @return {number}
111
 */
112
function read40Bit(bytes, i) {
113
    return readBytesAsBits(bytes, i, 5);
114
}
115
116
/**
117
 * Read 1 48-bit int from bytes.
118
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
119
 * @param {number} i The index to read.
120
 * @return {number}
121
 */
122
function read48Bit(bytes, i) {
123
    return readBytesAsBits(bytes, i, 6);
124
}
125
126
/**
127
 * Read 1 64-bit double from bytes.
128
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
129
 * @param {number} i The index to read.
130
 * @return {number}
131
 */
132
function read64BitFloat(bytes, i) {
133
    return floats.decodeFloat64(bytes.slice(i,i+8));
134
}
135
136
/**
137
 * Read 1 char from bytes.
138
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
139
 * @param {number} i The index to read.
140
 * @return {string}
141
 */
142
function readChar(bytes, i) {
143
    return String.fromCharCode(bytes[i]);
144
}
145
146
module.exports.readChar = readChar;
147
module.exports.read1Bit = read1Bit;
148
module.exports.read8Bit = read8Bit;
149
module.exports.read16Bit = read16Bit;
150
module.exports.read16BitFloat = read16BitFloat;
151
module.exports.read24Bit = read24Bit;
152
module.exports.read32Bit = read32Bit;
153
module.exports.read32BitFloat = read32BitFloat;
154
module.exports.read40Bit = read40Bit;
155
module.exports.read48Bit = read48Bit;
156
module.exports.read64BitFloat = read64BitFloat;
157